using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using WindowsLive.Writer.Api;

namespace ImFeelingLucky
{
    [WriterPlugin("6f118092-6d49-4e5c-8688-2cf6273bc502", "Insert I'm Feeling Lucky",
        Description = "Let's you put a Google I'm Feeling Lucky link into your blog",
        HasEditableOptions = true,
        PublisherUrl = "http://scottisafooldev.spaces.live.com/",
        ImagePath="google.gif")]
    [InsertableContentSource("I'm Feeling Lucky Link")]

    public class FeelingLucky : ContentSource
    {
        PluginSettings m_defaultsettings;

        public override void Initialize(IProperties pluginOptions)
        {
            base.Initialize(pluginOptions);
            m_defaultsettings = new PluginSettings(pluginOptions);
        }

        public override void EditOptions(IWin32Window dialogOwner)
        {
            PluginSettings opSettings = new PluginSettings(this.Options);
            frmOptions op = new frmOptions(opSettings);
            op.ShowDialog();            
        }

        public override DialogResult CreateContent(IWin32Window dialogOwner, ref string newContent)
        {
            string s = Regex.Replace(newContent, @"<(.|\n)*?>", string.Empty); ;
            if (!m_defaultsettings.LeaveFormatting)
            {
                newContent = s;
            }
            
            
            if (m_defaultsettings.AutoInsert && newContent != "")
            {
                string link = "";
                string temp = newContent;

                if (m_defaultsettings.OpenInNewWindow)
                {
                    link = " target=\"_blank\"";
                }

                newContent = string.Format("<a href=\"http://www.google.com/search?btnI=I'm Feeling Lucky&q={0}\"{1}>{2}</a>", s, link, temp);

                return DialogResult.OK;
            }
            else
            {
                using (frmFeelingLucky lucky = new frmFeelingLucky(newContent))
                {
                    DialogResult result = lucky.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        newContent = lucky.getText;
                    }
                    return result;
                }
            }
            
        }
    }
            
}
